package com.mediacloud.app.newsmodule.fragment.audio.live

import android.graphics.Color
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chinamcloud.project.shanshipin.activity.TikTokActivity
import com.mediacloud.app.model.interfaces.DataInvokeCallBack
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.model.news.ArticleListData
import com.mediacloud.app.model.news.BaseMessageReciver
import com.mediacloud.app.model.utils.LogUtil
import com.mediacloud.app.model.utils.ViewUtils
import com.mediacloud.app.model.view.floatwin.FloatButtonGroup
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.activity.AreaLiveListActivity
import com.mediacloud.app.newsmodule.activity.microlive.BaseRecyclerAdapter
import com.mediacloud.app.newsmodule.adaptor.video.vod.SmallVideoListAdapter
import com.mediacloud.app.newsmodule.fragment.nav2.BaseRecyclerListFragment
import com.mediacloud.app.newsmodule.utils.LoginUtils
import com.mediacloud.app.newsmodule.utils.NewsListDataInvoker
import com.mediacloud.app.newsmodule.utils.SpaceItemDecoration
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.utils.DisplayUtil

/**
Date:2020/4/21
Author:Wy
 */
class AudioLiveAreaListFragment : BaseRecyclerListFragment<AudioLiveAreaListAdapter>(), BaseRecyclerAdapter.ItemClickListener {


    var dataInvoker: NewsListDataInvoker? = null
    var dataCallback: BaseDataInvokeCallBack<out BaseMessageReciver?>? = null

    val LOGIN = 1
    val REFRESH = 2
    var Mode = REFRESH

    override fun getLayoutResID(): Int {
        return R.layout.fragment_audio_live_area_list
    }
    override fun getRecyclerAdapter(): AudioLiveAreaListAdapter? {
        recyclerView.addItemDecoration(  GridSpacingItemDecoration(4,
                resources.getDimensionPixelSize(R.dimen.dimen13),resources.getDimensionPixelSize(R.dimen.dimen13), false))
        val movieGridAdapter = AudioLiveAreaListAdapter(activity)
        movieGridAdapter.setItemClickListener(this)
        return movieGridAdapter
    }

    override fun getLayoutManager(): RecyclerView.LayoutManager? {
        val layoutManager = GridLayoutManager(activity, 4)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        return layoutManager
    }

    override fun initView() {
        super.initView()
        recyclerView.setBackgroundColor(Color.parseColor("#FFF4F4F4"))
        retryTips?.setOnClickListener {
            if (Mode == LOGIN){
                LoginUtils.request_source = "视频播放配置";
                LoginUtils.invokeLogin(activity) }
           else {
                showStateView(TYPE_LOADING, false)
                refresh()
            }
        }
        refresh()
    }

    override fun initArgs() {
        super.initArgs()
        initCallBackInstance()
        initDataInvoker()
    }

    protected fun refresh() {
        pageIndex = 1
        load()
    }

    protected fun loadMore() {
        pageIndex++
        load()
    }

    private fun load() {
        Mode = REFRESH
        dataInvoker?.getContentListWithAd(catalogID, pageIndex, perPageSize,null)
    }


    protected fun initDataInvoker() {
        dataInvoker = NewsListDataInvoker(dataCallback)
    }


    protected fun getDataInvokeCallBackClass(): Class<out BaseDataInvokeCallBack<out BaseMessageReciver?>?> {
        return BaseNewsDataInvokeCallBack::class.java
    }


    private fun initCallBackInstance() {
//        val classRef: Class<*> = getDataInvokeCallBackClass()
        try {
//            dataCallback = classRef.constructors[0].newInstance(this) as BaseDataInvokeCallBack<out BaseMessageReciver?>
            dataCallback = BaseNewsDataInvokeCallBack()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onItemClick(index: Int, isFromAdaptor: Boolean, adapter: BaseRecyclerAdapter<*>?) {

        AreaLiveListActivity.startActivity(requireContext(),recyclerAdapter.data[index])
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        loadMore()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        refresh()
    }


    inner abstract class BaseDataInvokeCallBack<T> : DataInvokeCallBack<T> {
        override fun fault(data: Any?) {
            refreshLayout.finishRefresh(false)
            refreshLayout.finishLoadMore()
            //            refreshLayout.setRefreshing(false);
            pageIndex--;
            if (data != null && recyclerAdapter != null && (recyclerAdapter.getData() == null
                            || recyclerAdapter.getData().size == 0)) {
                showStateView(TYPE_NET_NOT_GIVE_FORCE, false)
            } else if (recyclerAdapter != null && (recyclerAdapter.getData() == null
                            || recyclerAdapter.getData().size == 0)) {
                showStateView(TYPE_NO_CONTENT, false)
            } else  // closeStateView()
                showStateView(TYPE_NO_CONTENT, false)
            ViewUtils.showToast(getActivity(), "获取数据失败") //,Snackbar.LENGTH_SHORT).show();
        }
    }


    inner class BaseNewsDataInvokeCallBack : BaseDataInvokeCallBack<ArticleListData?>() {
        override fun success(result: ArticleListData?) {
            if (pageIndex == 1) refreshLayout.finishRefresh()
            if (result?.state!!) {


                result?.let {
                    if (!result.more) refreshLayout.finishLoadMoreWithNoMoreData() else refreshLayout.finishLoadMore()
                    //必须 的
                    val floatData = result.orginData.optJSONObject("data")
                    if (pageIndex == 1) {
                        if (FloatButtonGroup.haveFloatField(floatData)) {
                            initFloatWin(floatData)
                        } else {
                            hideFloatWin()
                        }
                    }
                    if (result.articleList != null) {
                        closeStateView()
                        if (pageIndex == 1) recyclerAdapter.getData().clear()
                        recyclerAdapter.data.addAll(result.articleList)
                        val size: Int = recyclerAdapter.getItemCount()
                        LogUtil.i("result.componentItems " + result.componentItems.size)
                        // https://zentao.chinamcloud.com/zentao/bug-view-29560.html 不显示
                        /* try {
                             addComponent(result.componentItems)
                         } catch (e: Exception) {
                             e.printStackTrace()
                         }*/
                        if (pageIndex == 1) {
                            recyclerView.notifyDataSetChanged()
                        } else {
                            recyclerView.notifyItemRangeInserted(size, result.articleList.size)
                        }
                    } else {
                        fault(null)
                    }
                }
            } else {
                fault(null)
            }

        }

    }


    override fun onDestroy() {
        super.onDestroy()
        if (dataInvoker != null) {
            dataInvoker!!.destory()
        }
    }
}